//
//  NavigationController.m
//  Mango
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "NavigationController.h"

@interface NavigationController ()
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation NavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateSkinModel];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    self.navigationBar.tintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}


-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark 更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    NSString *currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.navigationBar.barTintColor = [UIColor colorWithRed:34/255.0 green:30/255.0 blue:33/255.0 alpha:1.0];
        attributes[NSForegroundColorAttributeName] = [UIColor grayColor];
        self.toolbar.barTintColor = [UIColor blackColor];
        self.navigationBar.tintColor = [UIColor grayColor];
    }else {//日间模式
        self.navigationBar.barTintColor = [UIColor colorWithRed:252/255.0 green:65/255.0 blue:19/255.0 alpha:1.0];
        self.navigationBar.tintColor = [UIColor whiteColor];
        attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
        self.toolbar.barTintColor = [UIColor whiteColor];
    }
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    self.navigationBar.titleTextAttributes = attributes;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * 可以在这个方法中拦截所有push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setTitle:@"返回" forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"NavigationButtonReturnGray"] forState:UIControlStateNormal];
         self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
        if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {
            [button setImage:[UIImage imageNamed:@"navigationButtonReturnGray"] forState:UIControlStateNormal];
        }else {
            [button setImage:[UIImage imageNamed:@"navigationButtonReturnWhite"] forState:UIControlStateNormal];
        }
//        [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        button.size = CGSizeMake(70, 40);
        // 让按钮内部的所有内容左对齐
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        [button sizeToFit];
        // 让按钮的内容往左边偏移10
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        // 修改导航栏左边的item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        // 隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
    [super pushViewController:viewController animated:animated];
    
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
